<?php
/**
 * 购物车接口
 * @author maniac
 */
namespace app\api\controller\v1;

use app\facade\Buycar AS BuycarFacade;

class Buycar extends Base
{
    /**
     * 加入购物车
     */
    public function add()
    {
        $post = input('post.');

        if (!isset($post['goods_id']) || !$post['goods_id']) {
            return j(1, '请选择商品');
        }
        $post['goods_sku_id']   = isset($post['goods_sku_id']) ? $post['goods_sku_id'] : '';
        $post['goods_sku_name'] = isset($post['goods_sku_name']) ? $post['goods_sku_name'] : '';
        if (!isset($post['goods_nums']) || !(int)$post['goods_nums']) {
            return j(1, '请选择商品数量');
        }
//        try {
            $post['userid'] = $this->checkUserToken();
            $data = BuycarFacade::add($post);
            return j(0, '加入购物车成功', $data);
//        } catch (\Exception $e) {
//            return j(1, $e->getMessage());
//        }
    }
    
    /**
     * 购物车列表
     */
    public function buycarList()
    {

        $type = input('post.type',0);//类型 0为单商户1为多商户
        $id=input('post.id','');

        if(!empty($id)){

            $id=explode(',',$id);

        }

//        try {
            $userId = $this->checkUserToken();
            $list = BuycarFacade::buycarList($userId,$type,$id);
            return j(0, '获取购物车列表成功', $list);
//        } catch (\Exception $e) {
//            return j(1, $e->getMessage());
//        }
    }

    
    public function editCart()
    {
        $userId    = $this->checkUserToken();
        $id        = input('post.id', 0);
        $goodsNums = input('post.goods_nums', 0);
        if (!$id) {
            return j(1, '请传入要修改的商品编号和数量');
        }
        try {
            $list = BuycarFacade::editCart($id, $goodsNums);
            return j(0, '编辑成功');
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }
}
